module one {
	export function getSoundKey(name: string): string {
		if (RES.hasRes(name)) {
			return name;
		}

		if (egret.Capabilities.os == "Android") {
			if (RES.hasRes(name + "_ogg")) {
				return name + "_ogg";
			}
		}
		else if (egret.Capabilities.os == "iOS") {
			if (RES.hasRes(name + "_m4a")) {
				return name + "_m4a";
			}
		}

		if (RES.hasRes(name + "_mp3")) {
			return name + "_mp3";
		}

		return null;
	}

	export function getSound(name: string): egret.Sound {
		let sound: egret.Sound;
		if (egret.Capabilities.os == "Android") {
			sound = RES.getRes(name + "_ogg")
			if (sound) {
				return sound;
			}
		}
		else if (egret.Capabilities.os == "iOS") {
			sound = RES.getRes(name + "_m4a")
			if (sound) {
				return sound;
			}
		}

		sound = RES.getRes(name + "_mp3")
		if (sound) {
			return sound;
		}

		return RES.getRes(name);
	}

	export class SoundManager {
		public static defaultMusicVolume: number = 1;
		public static defaultSoundVolume: number = 1;

		public constructor() {
			this.musicCtr = new MusicCtr();
			this.soundCtr = new SoundCtr();
		}

		private getKey(type: string): string {
			return `${this.project}_${type}_not_open`;
		}

		public isOpened(type: string): boolean {
			return !egret.localStorage.getItem(this.getKey(type));
		}

		private project: string = "";
		public init(project: string): void {
			this.project = project;

			let isMusicOpened = this.isOpened(egret.Sound.MUSIC);
			let isEffectOpened = this.isOpened(egret.Sound.EFFECT);

			if (isMusicOpened) {
				this.musicCtr.open();
			}
			else {
				this.musicCtr.close();
			}
			if (isEffectOpened) {
				this.soundCtr.open();
			}
			else {
				this.soundCtr.close();
			}
		}

		musicCtr: MusicCtr;
		public playMusic(name: string, duration: number = 0): void {
			this.musicCtr.play(name, 0, -1, duration);
		}

		public playLastMusic(): void {
			this.musicCtr.playLastMusic();
		}

		private soundCtr: SoundCtr;
		public playSound(name: string, startTime: number, playTimes: number): number {
			return this.soundCtr.play(name, startTime, playTimes);
		}

		public stopSound(count: number): void {
			this.soundCtr.stop(count);
		}

		public playSounds(keys: string[]): void {
			this.soundCtr.playSounds(keys);
		}

		public stop(type: string):void {
			if (type == egret.Sound.MUSIC) {
				this.musicCtr.stopAll();
			}
			else {
				this.soundCtr.stopAll();
			}
		}

		open(type: string): void {
			if (type == egret.Sound.MUSIC) {
				this.musicCtr.open();
			}
			else {
				this.soundCtr.open();
			}
			egret.localStorage.setItem(this.getKey(type), "");
		}

		close(type: string): void {
			if (type == egret.Sound.MUSIC) {
				this.musicCtr.close();
			}
			else {
				this.soundCtr.close();
			}
			egret.localStorage.setItem(this.getKey(type), "1");
		}

		setVolume(type: string, v: number): void {
			if (type == egret.Sound.MUSIC) {
				this.musicCtr.volume = v;
			}
			else {
				this.soundCtr.volume = v;
			}
		}

		getVolume(type: string): number {
			if (type == egret.Sound.MUSIC) {
				return this.musicCtr.volume;
			}
			else {
				return this.soundCtr.volume;
			}
		}
	}

	export var soundManager: SoundManager = new SoundManager();
}